<!--
 * @Author: firesky
 * @Date: 2020-08-31 09:49:57
 * @LastEditTime: 2021-01-19 11:30:33
 * @LastEditors: firesky
-->
<template>
  <div class="app-container">
    <div>
      <el-row class="admin-top">
        <el-input v-model="key" clearable style="width: 195px;" placeholder="请输入内码" @keyup.native.enter="searchUser" @change="searchUser"/>
        <el-button type="primary" icon="el-icon-search" @click="searchUser">搜索</el-button>
        <el-button style="float: right" icon="el-icon-plus" type="success" @click="addUser">添加</el-button>
      </el-row>
    </div>
    <br>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="正在加载"
      border
      fit
      highlight-current-row>
      <el-table-column label="记录内码" align="center">
        <template slot-scope="scope">
          {{ scope.row.FID }}
        </template>
      </el-table-column>
      <el-table-column label="对象代码" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRepKey }}
        </template>
      </el-table-column>
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRepIndex }}
        </template>
      </el-table-column>
      <el-table-column label="页号" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRepPage }}
        </template>
      </el-table-column>
      <el-table-column label="字段标题" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRepCap }}
        </template>
      </el-table-column>
      <el-table-column label="字段名" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRepFld }}
        </template>
      </el-table-column>
      <el-table-column label="字段类型" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRepType }}
        </template>
      </el-table-column>
      <el-table-column label="是否隐藏" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRepHide }}
        </template>
      </el-table-column>
      <el-table-column label="合计" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRepSum }}
        </template>
      </el-table-column>
      <el-table-column label="宽度" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRepWid }}
        </template>
      </el-table-column>
      <el-table-column label="权限键值" align="center">
        <template slot-scope="scope">
          {{ scope.row.FRightKey }}
        </template>
      </el-table-column>
      <el-table-column label="小数位" align="center">
        <template slot-scope="scope">
          {{ scope.row.FDecPlaces }}
        </template>
      </el-table-column>
      <el-table-column align="center" width="160" label="操作" >
        <template slot-scope="scope">
          <el-button type="text" @click="insertTable(scope.row)">插表</el-button>
          <el-button type="text" @click="editUser(scope.row)">编辑</el-button>
          <el-button type="text" @click="delUser(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :total="total"
      :page-sizes="[100, 300, 500, 1000, 2000, 3000]"
      :page-size="pagesize"
      :current-page.sync="currentPage"
      class="jy-pagination"
      style="text-align: center; margin-top: 20px;"
      background
      layout="sizes, prev, pager, next, total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"/>

    <el-dialog :visible.sync="addUserModal" title="添加／编辑">
      <el-form v-loading="loadingForm" ref="form" :rules="rules" :model="form" label-width="120px">
        <el-form-item label="对象代码" prop="FRepKey">
          <el-input v-model="form.FRepKey" :disabled="isEditor"/>
        </el-form-item>
        <el-form-item label="序号" prop="FRepIndex">
          <el-input v-model="form.FRepIndex" :disabled="isEditor"/>
        </el-form-item>

        <el-form-item label="页号" prop="FRepPage">
          <el-input v-model="form.FRepPage" />
        </el-form-item>
        <el-form-item label="字段标题" prop="FRepCap">
          <el-input v-model="form.FRepCap" />
        </el-form-item>
        <el-form-item label="字段名" prop="FRepFld">
          <el-input v-model="form.FRepFld" />
        </el-form-item>
        <el-form-item label="字段类型" prop="FRepType">
          <el-input v-model="form.FRepType" />
        </el-form-item>
        <el-form-item label="隐藏" prop="FRepHide">
          <el-radio-group v-model="form.FRepHide">
            <el-radio :label="0">是</el-radio>
            <el-radio :label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="合计" prop="FRepSum">
          <el-input v-model="form.FRepSum" />
        </el-form-item>
        <el-form-item label="宽度" prop="FRepWid">
          <el-input v-model="form.FRepWid" />
        </el-form-item>
        <el-form-item label="权限键值" prop="FRightKey">
          <el-input v-model="form.FRightKey" />
        </el-form-item>
        <el-form-item label="小数位" prop="FDecPlaces">
          <el-input v-model="form.FDecPlaces" />
        </el-form-item>

        <el-form-item>
          <el-button :loading="loadingForm" type="primary" @click="confirm" >确认</el-button>
          <el-button @click="addUserModal=false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>
<script>
import { tableConfig, configAdd, configUpdate, insertColumn } from '../../api/account'
export default {
  inject: ['globalDb'],
  data() {
    return {
      key: '',
      list: [],
      listLoading: false,
      total: 0,
      currentPage: 1,
      pagesize: 10,
      addUserModal: false,
      loadingForm: false,
      isEditor: false,
      form: {
        FRepKey: '',
        FRepIndex: '',
        FRepPage: '',
        FRepCap: '',
        FRepFld: '',
        FRepType: '',
        FRepHide: 0,
        FRepSum: '',
        FRepWid: '',
        FRightKey: '',
        FDecPlaces: ''
      },
      rules: {

      }
    }
  },
  mounted() {
    this.getTableConfig()
  },
  methods: {
    searchUser() {
      this.getTableConfig()
    },
    async getTableConfig() {
      try {
        this.listLoading = true
        const res = await tableConfig({
          db: this.globalDb.db,
          opername: decodeURIComponent(this.globalDb.fname),
          key: 12001,
          limit: this.pagesize,
          page: this.currentPage
        })
        this.list = res.Data
        this.listLoading = false
        // console.log(res, ' res')
      } catch (error) {
        this.listLoading = false
        console.log(error)
      }
    },
    addUser() {
      const data = {
        FRepKey: '',
        FRepIndex: '',
        FRepPage: '',
        FRepCap: '',
        FRepFld: '',
        FRepType: '',
        FRepHide: 0,
        FRepSum: '',
        FRepWid: '',
        FRightKey: '',
        FDecPlaces: ''
      }
      for (const i in this.form) {
        this.$set(this.form, i, data[i])
      }
      console.log(this.form, ' this.form')
      setTimeout(() => {
        this.$refs['form'].clearValidate()
      })

      this.addUserModal = true
    },
    confirm() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          this.loadingForm = true
          try {
            console.log(this.form, ' this.form')
            this.form.db = this.globalDb.db
            let res = ''
            if (this.form.FID) {
              res = await configUpdate(this.form)
            } else {
              res = await configAdd(this.form)
            }
            if (res.Error === 0) {
              this.loadingForm = false
              this.addUserModal = false
              console.log(res, ' res add')
              this.$message({
                message: res.Message,
                type: 'success'
              })
              this.getTableConfig()
            }
          } catch (error) {
            this.loadingForm = false
            console.log(error)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    editUser(data) {
      this.addUserModal = true
      for (const i in this.form) {
        this.form[i] = data[i]
      }
      // console.log('data：', data)
      // console.log('this.form：', this.form)
      this.form.FID = data.FID
      console.log('this.form：', this.form)
      setTimeout(() => {
        this.$refs['form'].clearValidate()
      })
    },
    async insertTable(data) {
      console.log(data, ' data')
      const { FRepFld, FRepKey } = data
      const res = await insertColumn({
        db: this.globalDb.db,
        opername: decodeURIComponent(this.globalDb.fname),
        FRepFld,
        FRepKey
      })

      // console.log(res, ' res insert')
      if (res.Error === 0) {
        this.$message({
          message: res.Message,
          type: 'success'
        })
        this.getTableConfig()
      }
    },
    handleSizeChange(val) {
      this.currentPage = 1
      this.pagesize = val
      this.getTableConfig()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getTableConfig()
    }
  }
}
</script>
